package cn.rongdao.jrkabao.ui.loan.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRImageLoad;

import java.util.ArrayList;
import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.information.apply.ApplyInfoBean;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.ui.information.listener.OnItemActionListener;

/**
 * Created by Wang on 17/9/19.
 */

public class CreditLoanMaterialAdapter extends RecyclerView.Adapter<CreditLoanMaterialAdapter.ViewHolder> {
    private List<ApplyInfoBean.MustListEntity> metaialBeanList = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    private GridLayoutManager gridLayoutManager;
    private OnItemActionListener meOnClickListener;
    public static final int SPAN_COUNT_ONE = 1;
    public static final int SPAN_COUNT_OTHER = 6;
    private MeOnClickListener clickListener;

    public CreditLoanMaterialAdapter(Context context, GridLayoutManager gridLayoutManager) {
        this.context = context;
        this.gridLayoutManager = gridLayoutManager;
        inflater = LayoutInflater.from(context);
    }


    public void setData(List<ApplyInfoBean.MustListEntity> List, boolean isrefresh) {
        if (isrefresh) {
            metaialBeanList.clear();
        }
        metaialBeanList.addAll(List);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        int spanCount = gridLayoutManager.getSpanCount();
        if (spanCount == SPAN_COUNT_ONE) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            //竖直
            View view = inflater.inflate(R.layout.item_creditloans_material, parent, false);
            return new ViewHolder(view, viewType);
        } else {
            //水平
            View view = inflater.inflate(R.layout.item_creditloans_material_vercatial, parent, false);
            return new ViewHolder(view, viewType);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final ApplyInfoBean.MustListEntity bean = metaialBeanList.get(position);
        if (holder.viewType == 0) {
            //竖直列表
            String code = bean.getCode();
            String cn = bean.getCn();
            holder.name.setText(cn);
            String msg = bean.getMsg();
            FRImageLoad.loadImage(bean.getLogo(), holder.icon);
            int status = bean.getStatus();
            holder.state.setTextColor(getTextColor(0));
            holder.progressBar.setVisibility(View.GONE);
            //1 通过 -1 正在验证 2 实名不匹配  0  没用填过资料1:
            switch (status) {
                case -1:
                    holder.state_img.setVisibility(View.GONE);
                    holder.state.setVisibility(View.VISIBLE);
                    holder.state.setText("正在验证中,请稍候...");
                    holder.image_complete.setVisibility(View.GONE);
                    holder.progressBar.setVisibility(View.VISIBLE);
                    break;
                case 0:
                    holder.state_img.setVisibility(View.GONE);
                    holder.state.setVisibility(View.VISIBLE);
                    holder.state.setText("去验证");
                    holder.state.setTextColor(getTextColor(1));
                    holder.image_complete.setVisibility(View.GONE);
                    break;
                case 1:
                    holder.state.setVisibility(View.VISIBLE);
                    holder.state.setText("已完善");
                    holder.state_img.setVisibility(View.GONE);
                    holder.image_complete.setVisibility(View.VISIBLE);
                    holder.image_complete.setImageResource(getStateImg(holder,1));
                    break;
                case 2:
                    holder.state.setVisibility(View.VISIBLE);
                    holder.state.setText(msg);
                    holder.state.setTextColor(getTextColor(1));
                    holder.state_img.setVisibility(View.VISIBLE);
                    holder.image_complete.setVisibility(View.VISIBLE);
                    holder.image_complete.setImageResource(getStateImg(holder,2));
                    break;
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (meOnClickListener != null) {
                        initListener(bean);
                    }
                }
            });
        } else {
            //横着的列表

            String code = bean.getCode();
            String cn = bean.getCn();
            FRImageLoad.loadImage(bean.getLogo(), holder.v_icon);
            int status = bean.getStatus();
            holder.v_name.setText(cn);
            if (code.equals(InfoConstant.IDENTITY_PIC )) {
                holder.v_name.setText("身份证");
            }
            if (code.equals(InfoConstant.OPERATOR_VERIFY)) {
                holder.v_name.setText("运营商");
            }
            /*if(position ==0){
                status = 1;
            }
            if(position ==1){
                status =-1;
            }
            if(position == 2){
                status = 2;
            }*/
            //1 通过 -1 正在验证 2 实名不匹配  0  没用填过资料1:

            holder.v_image_complete.setVisibility(View.GONE);
            holder.v_progressBar.setVisibility(View.GONE);
            switch (status) {
                case -1:
                    holder.v_image_complete.setVisibility(View.GONE);
                    holder.v_progressBar.setVisibility(View.VISIBLE);
                    break;
                case 0:

                    break;
                case 1:
                    holder.v_image_complete.setVisibility(View.VISIBLE);
                    holder.v_image_complete.setImageResource(getStateImg(holder,1));
                    break;
                case 2:
                    holder.v_image_complete.setVisibility(View.VISIBLE);
                    holder.v_image_complete.setImageResource(getStateImg(holder,2));
                    break;
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (meOnClickListener != null) {
                        initListener(bean);
                    }
                }
            });
        }
    }

    private void initListener(ApplyInfoBean.MustListEntity mustListEntity) {
        if (initH5ClickListener(mustListEntity)) return;
        initNativeClickListener(mustListEntity);
    }


    private void initNativeClickListener(ApplyInfoBean.MustListEntity mustListEntity) {
        switch (mustListEntity.getCode()) {
            case InfoConstant.IDENTITY_INFO://身份信息
                meOnClickListener.onIdentityInfoClick(mustListEntity.getStatus());
                break;
            case InfoConstant.BASIC_INFO://基本资料
                meOnClickListener.onBasicInfoClick(mustListEntity.getStatus());
                break;
            case InfoConstant.CREDIT_VERIFY://信用卡
                meOnClickListener.onCreditVerifyClick(mustListEntity.getStatus());
                break;
            case InfoConstant.DEPOSIT_CARD://储蓄卡
                meOnClickListener.onDepositCardClick(mustListEntity.getStatus());
                break;
            case InfoConstant.EC_VERIFY://电商
                meOnClickListener.onECVerifyClick(mustListEntity.getStatus());
                break;
            case InfoConstant.IDENTITY_PIC://身份证照骗
                meOnClickListener.onIdentityPicClick(mustListEntity.getStatus());
                break;
            case InfoConstant.OPERATOR_VERIFY://手机号验证
                meOnClickListener.onOperatorVerifyClick(mustListEntity.getStatus());
                break;
            case InfoConstant.PERSONAL_CREDIT://个人征信
                meOnClickListener.onPersonalCreditClick(mustListEntity.getStatus());
                break;
            case InfoConstant.PUBLIC_ACCUMULATION_FUND://公积金
                meOnClickListener.onPublicAccumulationFundClick(mustListEntity.getStatus());
                break;
            case InfoConstant.ZHIMA://芝麻信用
                meOnClickListener.onZhimaClick(mustListEntity.getStatus());
                break;
            case InfoConstant.JD_VERIFY://京东
                meOnClickListener.onJingdongClick(mustListEntity.getStatus());
                break;
            case InfoConstant.OTHER_FILM_PROFILE://资产证明
                meOnClickListener.onOtherFilmprofileClick(mustListEntity.getStatus());
                break;
            case InfoConstant.ALIPAY://资产证明
                meOnClickListener.onAlipayClick(mustListEntity.getStatus());
                break;
            case InfoConstant.SECURITY_VERIFY://社保
                meOnClickListener.onSecurityVerifyClick(mustListEntity.getStatus());
                break;


        }
    }

    private boolean initH5ClickListener(ApplyInfoBean.MustListEntity mustListEntity) {
        if (mustListEntity.getJump_type() == 1) {
            meOnClickListener.onItemClickWithUrl(mustListEntity.getStatus(), mustListEntity.getJump_url(), mustListEntity.getCode());
            return true;
        }
        return false;
    }

    @Override
    public int getItemCount() {
        if (gridLayoutManager.getSpanCount() != SPAN_COUNT_ONE && metaialBeanList.size() > 6) {
            return 6;
        }
        return metaialBeanList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView v_name;
        private TextView state;
        private ImageView icon;
        private ImageView v_icon;
        private View line;
        private ImageView state_img;
        private int viewType;
        private ImageView image_complete;
        private ImageView v_image_complete;
        private ProgressBar progressBar;
        private ProgressBar v_progressBar;

        public ViewHolder(View itemView, int viewType) {
            super(itemView);
            this.viewType = viewType;
            if (viewType == 0) {
                name = (TextView) itemView.findViewById(R.id.item_creditcard_name);
                icon = (ImageView) itemView.findViewById(R.id.item_creditcard_icon);
                state = (TextView) itemView.findViewById(R.id.item_creditcard_state);
                line = itemView.findViewById(R.id.item_creditcard_line);
                state_img = (ImageView) itemView.findViewById(R.id.item_creditcard_state_img);
                image_complete = (ImageView) itemView.findViewById(R.id.item_creditcard_icon_complete);
                progressBar = (ProgressBar) itemView.findViewById(R.id.item_creditcard_vertical_icon_progress);
            } else {
                v_name = (TextView) itemView.findViewById(R.id.item_creditcard_vertical_name);
                v_icon = (ImageView) itemView.findViewById(R.id.item_creditcard_vertical_icon);
                v_image_complete = (ImageView) itemView.findViewById(R.id.item_creditcard_vertical_icon_complete);
                v_progressBar = (ProgressBar) itemView.findViewById(R.id.item_creditcard_vertical_progress);
            }
        }
    }

    private int getStateImg(ViewHolder holder,int tag){
        //1 通过 -1 正在验证 2 实名不匹配  0  没用填过资料1:
        switch (tag) {
            case -1:
                return  R.drawable.icon_commit_yanzheng;

            case 1:
                return  R.drawable.icon_commit_complete;
            case 2:
                return  R.drawable.icon_commit_nopass;
        }
        return R.drawable.icon_commit_nopass;
    }

    /*获取字体的颜色*/
    private int getTextColor(int tage) {
        if (tage == 0) {
            return Color.parseColor("#b0b0b0");
        } else {
            return Color.parseColor("#ff5a6f");
        }
    }

    public void setOnItemActionListener(OnItemActionListener onItemActionListener) {
        this.meOnClickListener = onItemActionListener;
    }

    public interface MeOnClickListener {

        void meOnClickListener(View view, int position);
    }
}
